/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.portlet.editor.gui.view.settings.table;

import java.awt.Component;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import com.genia.toolbox.basics.editor.gui.AbstractGUI;
import com.genia.toolbox.basics.editor.model.document.VariableModel;
import com.genia.toolbox.portlet.editor.gui.PortletEditorGUI;

/**
 * The variable table cell renderer (used for attributes and parameters).
 */
@SuppressWarnings("serial")
public class VariableCellRenderer
    extends Component
    implements TableCellRenderer
{

  /**
   * The application portletEditorGUI.
   */
  private PortletEditorGUI portletEditorGUI;

  /**
   * The standard font.
   */
  private Font usualFont = null;

  /**
   * The bold font.
   */
  private Font boldFont = null;



  /**
   * Constructor.
   * 
   * @param portletEditorGUI
   *          the application portletEditorGUI.
   */
  public VariableCellRenderer(PortletEditorGUI portletEditorGUI)
  {
    super();
    this.portletEditorGUI = portletEditorGUI;
  }



  /**
   * Handle the display.
   * 
   * @param table
   *          The table.
   * @param value
   *          The displayed value.
   * @param isSelected
   *          Whether the cell is selected.
   * @param hasFocus
   *          Whether the cell has the focus.
   * @param row
   *          The row index.
   * @param column
   *          The column index.
   * @return the component.
   */
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  {

    VariableTableModel tableModel = (VariableTableModel) table.getModel();
    VariableModel variable = tableModel.getValueAt(row);

    JLabel label = new JLabel();
    if (column == 0) {
      if (this.usualFont == null && this.boldFont == null) {
        this.usualFont = label.getFont();
        this.boldFont = new Font(label.getFont().getName(), Font.BOLD, label.getFont().getSize());
      }
      String name = null;
      try {
        name = this.portletEditorGUI.getController().notifyTranslation(variable.getName());
      }
      catch (Exception e) {
        name = variable.getName();
      }
      label.setText(" " + name);
      if (!isSelected) {
        label.setForeground(table.getForeground());
        label.setBackground(table.getBackground());
      }
      if (variable.isMandatory()) {
        label.setFont(this.boldFont);
        if (variable.getValue() == null || variable.getValue().trim().length() == 0) {
          label.setForeground(AbstractGUI.COLOR_TEXT_ERROR);
        }
      }
      else {
        label.setFont(this.usualFont);
      }
    }
    else {
      if (variable.getValue() != null && variable.getValue().trim().length() != 0) {
        label.setText(variable.getValue());
      }
      else {
        label.setText(null);
      }
      if (!isSelected) {
        label.setForeground(table.getForeground());
        label.setBackground(table.getBackground());
      }
    }

    return label;
  }

}
